<?php 
	require_once("config.php");
	
	if(isSet($_SESSION["id"])){
		header("Location: home.php");
	}
	
	if(isSet($_REQUEST["email"]) AND isSet($_REQUEST["name"])){
		//loob uut kasutajat
		
		$notice = $user->addUser(	$_REQUEST["name"],
						$_REQUEST["phone"],
						$_REQUEST["email"],
						$_REQUEST["password"]
						);
						
		if($notice != ""){
			// kasutaja loomine ebaõnnestus
			echo $notice;
		}else{
			echo "Kasutaja loodud";
		}
	
	}elseif(isSet($_REQUEST["email"]) AND isSet($_REQUEST["password"]) AND !isSet($_REQUEST["name"])){
		//kasutaja logib sisse
		$profile = $user->loginUser($_REQUEST["email"], $_REQUEST["password"]);
		
		// kas õnnestus
		if(empty($profile)){
			echo "Vale kasutajanimi või parool";
		}else {
			echo "Tere tulemast id:$profile->id nimi:$profile->name";
			
			$_SESSION["id"] = $profile->id;
			$_SESSION["name"] = $profile->name;
			
			header("Location: home.php");
			
		}
			
		
		
	}
	
	
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Login</title>
</head>
<body>
	<h2>Login</h2>
	<form action="login.php">
		<input autofocus type="email" name="email" placeholder="E-mail"><br>
		<input type="password" name="password" placeholder="Password"><br>
		<input type="submit" value="Sign in">
	</form><br>
	<a href="?create_user">Create User</a>
	<?php if(isSet($_REQUEST["create_user"])): ?>
		<h2>Create User</h2>
		<form action="login.php">
			<label for="name">Full name</label><br>
			<input type="text" name="name" id="name"><br>
			<label for="number">Phone</label><br>
			<input type="text" name="phone" id="phone"><br>
			<br>
			<label for="email">E-mail</label><br>
			<input type="email" name="email" id="email"><br>
			<label for="password">Password</label><br>
			<input type="password" name="password" id="password" ><br>
			<input type="submit" value="Sign up">
		</form>
	<?php endif; ?>
</body>
</html>